package com.tech.doggtv.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;

import com.tech.doggtv.R;
import com.tech.doggtv.action.OnItemClickListener;
import com.tech.doggtv.adapter.ColumnsAdapter;
import com.tech.doggtv.fragment.ProgramsListFragment;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * @Description: 节目列表类
 * Created by jl_luo on 2015/9/13.
 * email：dongyangluo@126.com
 */
public class VideoListActivity extends AppCompatActivity {

    private static final String TAG = VideoListActivity.class.getName();

    private String[] mColumnNames = new String[]{"运动","安静","展露类","综合类"};
    private String[] mColumnId = new String[]{"1","2","3","4"};
    @InjectView(R.id.rv_columns)
    RecyclerView mColumns;
    FrameLayout mListContent;
    private int currentPos = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_container);
        ButterKnife.inject(this);
        initColumns();
    }

    private void initColumns(){
        mColumns.setLayoutManager(new LinearLayoutManager(this));
        //添加分割线
//        mColumns.addItemDecoration(new DividerItemDecoration(
//                getActivity(), DividerItemDecoration.HORIZONTAL_LIST));
        ColumnsAdapter columnsAdapter =  new ColumnsAdapter(mColumnNames);
        columnsAdapter.setItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int pos) {
                Log.v(TAG, "~~~~~~~post=" + pos);
                if (pos == 0 && currentPos != 1) {
                    initFragment("1");
                } else if (pos == 1 && currentPos != 1) {
                    initFragment("2");
                } else if (currentPos != (pos+1)) {
                    initFragment((pos+1)+"");
                }
            }
        });
        mColumns.setAdapter(columnsAdapter);
        columnsAdapter.notifyDataSetChanged();

        initFragment("1");
    }
    FragmentManager fragmentManager;
    private void initFragment(String progType){
        Log.v(TAG,"~~~~~~~~~~~progType="+progType);
        if(fragmentManager==null) {
            fragmentManager = getSupportFragmentManager();
        }
        FragmentTransaction transaction =fragmentManager.beginTransaction();
//        if(progType.equals("1")){
//            currentPos = 1;
//            ProgramsListFragment fragment =  ProgramsListFragment.newInstance(progType);
//            transaction.replace(R.id.fg_content,fragment);
//            transaction.addToBackStack(null);
//            //提交修改
//            transaction.commit();
//        }else if(progType.equals("2")){
//            currentPos = 2;
//            ProgramsListFragment fragment =  ProgramsListFragment.newInstance(progType);
//            transaction.replace(R.id.fg_content,fragment);
//            transaction.addToBackStack(null);
//            //提交修改
//            transaction.commit();
//        }else{
            currentPos = Integer.parseInt(progType);
            ProgramsListFragment fragment =  ProgramsListFragment.newInstance(progType);
            transaction.replace(R.id.fg_content,fragment);
            transaction.addToBackStack(null);
            //提交修改
            transaction.commit();
//        }
    }
    @Override
    protected void onStop() {
        super.onStop();
        ButterKnife.reset(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        this.finish();
    }
}
